/* Copyright (c) 2004-2024 Peigen.info. All rights reserved. */

package info.peigen.hotpot.business.customer.core.service;

import cn.hutool.core.util.ObjectUtil;
import info.peigen.hotpot.business.customer.core.domain.Customer;
import info.peigen.hotpot.business.customer.core.domain.action.CustomerConverter;
import info.peigen.hotpot.business.customer.core.repository.service.CustomerRepository;
import info.peigen.hotpot.business.customer.core.repository.service.CustomerTechRepository;
import info.peigen.hotpot.business.customer.facade.CustomerQueryServiceFacade;
import info.peigen.hotpot.business.customer.facade.info.CustomerInfo;
import info.peigen.hotpot.business.customer.facade.order.query.CustomerPageOrder;
import info.peigen.hotpot.business.customer.facade.order.query.CustomerQueryOrder;
import info.peigen.hotpot.business.customer.facade.order.query.QueryCustomerSalesOrder;
import info.peigen.hotpot.business.customer.facade.order.query.QuerySalesCustomersOrder;
import info.peigen.hotpot.business.customer.facade.result.CustomerAccountsResult;
import info.peigen.hotpot.business.customer.facade.result.query.CustomerPageResult;
import info.peigen.hotpot.business.customer.facade.result.query.CustomerQueryResult;
import info.peigen.hotpot.business.customer.facade.result.query.QueryCustomerSalesResult;
import info.peigen.hotpot.business.customer.facade.result.query.QuerySalesCustomersResult;
import info.peigen.hotpot.common.service.base.ServiceQuery;
import info.peigen.hotpot.common.service.base.ServiceQueryContext;
import lombok.extern.slf4j.Slf4j;
import org.noear.solon.annotation.Component;
import org.noear.solon.annotation.Inject;

/**
 * <b>(CustomerAccountService)</b>
 *
 * @author Peigen
 * @version 1.0.0
 * @since 2022/2/9
 */
@Slf4j
@Component
public class CustomerQueryService extends AbstractCustomerQueryService implements CustomerQueryServiceFacade {
    @Inject
    CustomerRepository     customerRepository;
    @Inject
    CustomerTechRepository customerTechRepository;

    @Override
    public CustomerQueryResult queryCustomerInfo(CustomerQueryOrder order) {
        ServiceQueryContext serviceContext = ServiceQueryContext.builder().order(order).result(new CustomerQueryResult()).build();
        query(serviceContext, new ServiceQuery() {
            @Override
            public void query(ServiceQueryContext serviceContext) {
                CustomerQueryOrder order = (CustomerQueryOrder) serviceContext.getOrder();
                // 1. 接口查询
                serviceContext.setInfo(customerRepository.activeByOpenid(order.getOpenid()));
            }

            @Override
            public void convert(ServiceQueryContext serviceContext) {
                if (ObjectUtil.isNotEmpty(serviceContext.getInfo())) {
                    CustomerInfo customerInfo = CustomerConverter.convertCustomerInfo((Customer) serviceContext.getInfo());
                    ((CustomerQueryResult) serviceContext.getResult())
                            .fromSuccess(customerInfo);
                    // 3. 组装Result
                }
            }
        });
        return (CustomerQueryResult) serviceContext.getResult();
    }

    @Override
    public QuerySalesCustomersResult querySalesCustomers(QuerySalesCustomersOrder order) {
        return null;
    }

    @Override
    public QueryCustomerSalesResult queryCustomerSales(QueryCustomerSalesOrder order) {
        return null;
    }

    @Override
    public CustomerAccountsResult queryCustomerAccounts(CustomerQueryOrder order) {
        return null;
    }

    @Override
    public CustomerPageResult queryCustomerPage(CustomerPageOrder order) {
        return null;
    }
}
